define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var array = require("$UI/system/lib/base/array");
	var Model = function(){
		this.callParent();
	};
	var j=0;

	Model.prototype.getTexts = function(){
		var ret = [];
		var fID;
		var LoginState = localStorage.getItem('LoginState');
		if(this.params.shopID != undefined){
		  var data = this.comp('productData');
		  var shopid = this.params.shopID; 
		   data.each(function(param){
			   var row = param.row;
			   var ID = row.val('fID');
			   fID = ID.toString();
			   if(fID == shopid) {
				   ret.push(row);
			   }
			  });
		}else if(j==0){
			var val=[];var i=0;
			 justep.Baas.sendRequest({
			"url" : "/nase/nase_action",
			"action" : "queryNase_shopping",
			"async" : false,
			"params" : {},
			"success" : function(data) {
				array.each(data.rows, function(obj) {
					var fProID = obj.fProID.value;
					var fPhone = obj.fPhone.value;
					var fShop = obj.fShop.value;
					var fChoose = obj.fChoose.value;
					if(fPhone == LoginState && fShop =='待支付' && fChoose=='1'){
						val[i] = fProID;
						i++;
					}
				});
			}
		   });
			  var data = this.comp('productData');
			   data.each(function(param){
				   var row = param.row;
				   var id = row.val("fID");
				   for(var i=0;i<val.length;i++){
					   if(id == val[i]){
						   ret.push(row);
					   }
				   }
				  });
			
		}
		return ret;
	};
	
	
	//显示订单
	Model.prototype.modelParamsReceive = function(event){
		var LoginState = localStorage.getItem('LoginState');
		var num = 0;
		var pnum = 0;
		var shopid = this.params.shopID;
		if(shopid){
			justep.Baas.sendRequest({
			"url" : "/nase/nase_action",
			"action" : "queryNase_product",
			"async" : false,
			"params" : {},
			"success" : function(data) {
				array.each(data.rows, function(obj) {
					var ID = obj.fID.value;
					fID = ID.toString();
					if(fID == shopid){
						var fPrice = obj.fPrice.value;
						num = num + fPrice;
						var fPostage = obj.fPostage.value;
						pnum = pnum + fPostage;
					}
				});
			}
		   });
		}else{
			var val=[];var i=0;
			 justep.Baas.sendRequest({
			"url" : "/nase/nase_action",
			"action" : "queryNase_shopping",
			"async" : false,
			"params" : {},
			"success" : function(data) {
				array.each(data.rows, function(obj) {
					var fProID = obj.fProID.value;
					var fPhone = obj.fPhone.value;
					var fShop = obj.fShop.value;
					var fChoose = obj.fChoose.value;
					if(fPhone == LoginState && fShop =='待支付' && fChoose=='1'){
						val[i] = fProID;
						i++;
					}
				});
			}
		   });
			justep.Baas.sendRequest({
			"url" : "/nase/nase_action",
			"action" : "queryNase_product",
			"async" : false,
			"params" : {},
			"success" : function(data) {
				array.each(data.rows, function(obj) {
						var ID = obj.fID.value;
						fID = ID.toString();
						for(var i=0;i<val.length;i++){
							if(fID == val[i]){
								var fPrice = obj.fPrice.value;
								num = num + fPrice;
								var fPostage = obj.fPostage.value;
								pnum = pnum + fPostage;
							}
					}
				});
			}
		   });
		}
		  
			var money = this.getIDByXID('money');
			document.getElementById(money).innerHTML = '<span class="pull-right cB3MZ3y" style="color:#FF0000;font-size:16px;">￥'+num+'.00</span>';
			var Postage = this.getIDByXID('Postage');
			document.getElementById(Postage).innerHTML = '<span class="pull-right cB3MZ3y" style="color:#FF0000;font-size:12px;margin-right:10px;">￥'+pnum+'.00</span>';
			var Total = this.getIDByXID('Total');
			var s = num + pnum;
			document.getElementById(Total).innerHTML = '<span style="font-size:16px;color:#FF0000;">'+s+'.00</span>';
	};
	
	Model.prototype.confirm = function(event){
		justep.Shell.showPage("confirm");
	};
	//提交订单
	Model.prototype.buyClick = function(event){
		var LoginState = localStorage.getItem('LoginState');
		var id = localStorage.getItem('id');
		var shopData = this.comp("shopData");
		shopData.setFilter("fPhoneFilter","fPhone ='"+ LoginState +"'");
		shopData.setFilter("fProIDFilter","fProID ='"+ id +"'");
		shopData.setFilter("fShopFilter","fShop ='待支付'");
		shopData.refreshData();
		if(shopData.count() > 0){
			shopData.setValue("fShop", "");
			shopData.setValue("fComment", "已购买");
			shopData.saveData();
		}else{
			shopData.newData({
				"defaultValues" : [ {
					"fPhone" : localStorage.getItem('LoginState'),
					"fProID" : localStorage.getItem('id'),
					"fNumber": 1,
					"fShop" : "",
					"fComment":"已购买",
					"fChoose" : 0
				} ]
			});
			shopData.saveData();
		}
		justep.Shell.showPage("success");
	};
	
	
	Model.prototype.backBtnClick = function(event){
		var shopData =this.comp('shopData');
		var productData =this.comp('productData');
		shopData.each(function(param){
			var row = param.row;
			shopData.setValue("fChoose", 0);
		});
		shopData.saveData();
		justep.Shell.closePage();
	};
	
	Model.prototype.buy = function(row){
		if(this.params.shopID == undefined){
			var LoginState = localStorage.getItem('LoginState');
			return row.val('fPhone')== LoginState && row.val('fShop')=='待支付' && row.val('fChoose')=='1';
		}else{
			var LoginState = localStorage.getItem('LoginState');
			var shopid = this.params.shopID;
			return row.val('fPhone')== LoginState && row.val('fProID')== shopid;
		}
	};
	
	return Model;
});